package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：石破天
 * @date ：Created in 2022年12月20日
 * @description ：
 * @version: 1.0
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;


    @Override
    @Transactional  //操作了两张表，为保证数据一致，开启事务控制
    public void saveByDto(DishDto dishDto) {
        this.save(dishDto);

        //在sql添加完成后，就生成了菜品的id，可以get到了
        Long dishDtoId = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();

        if (flavors!=null && flavors.size()>0) {
            for (DishFlavor flavor : flavors) {
                //此时flavors集合中的  flavor  的dishid都是null
                //把获取到的菜品id写进去
                flavor.setDishId(dishDtoId);
            }
        }

        //批量操作
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdRDto(Long id) {
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();


        //操！！！！！没学过  爷想了1个小时
        BeanUtils.copyProperties(dish,dishDto);




        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);

        List<DishFlavor> flavors = dishFlavorService.list(wrapper);

        dishDto.setFlavors(flavors);


        return dishDto;
    }

    @Override
    public void updateByDto(DishDto dishDto) {
        this.updateById(dishDto);

        //先移除原来的口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(wrapper);

        Long dishDtoId = dishDto.getId();

        //移除后重现添加进去
        List<DishFlavor> flavors = dishDto.getFlavors();

        if (flavors!=null && flavors.size()>0) {
            for (DishFlavor flavor : flavors) {
                //此时flavors集合中的  flavor  的dishid都是null
                //把获取到的菜品id写进去
                flavor.setDishId(dishDtoId);
            }
        }

        //批量操作
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void removeAllInfoById(Long id) {



        this.removeById(id);

        //用dish 的Id去  dish_flavor表中删除
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);

        List<DishFlavor> flavors = dishFlavorService.list(wrapper);

        for (DishFlavor flavor : flavors) {
            dishFlavorService.removeById(flavor.getId());
        }
    }
}
